<template>
  <div class="app-container">
    <div class="toolbar">
      <el-button type="primary" @click="handleAddChapter">新增</el-button>
      <el-button type="danger" @click="handleDeleteChapter" :disabled="!multipleSelection.length"
        >删除</el-button
      >
    </div>
    <Tabel
      :tableData="tableData"
      :cols="cols"
      :isSelect="true"
      @selection-change="handleSelectionChange"
    >
      <template #operationCol>
        <!-- 树形图 -->
        <el-table-column type="expand" >
          <template slot-scope="scope" >
             <el-row   v-for="(item1,i) in ThreeData"  :key="i" class=" itemAligin">
               <template  v-if="scope.row.chapterId==item1.chapterId?true:false">
                      <!-- 一级渲染 -->
                      <el-col :span="5">
                          <el-tag  >
                            {{item1.chapterName}}
                          </el-tag>
                          <i class="el-icon-caret-right"></i>
                      </el-col>
                      <!-- 二级渲染 -->
                      <el-col :span="18"  >
                           <el-row v-for="(item2,i2) in item1.childrens" :key="i2"   class=" itemAligin">
                              <el-col :span="8" >
                                  <el-tag  type="warning" >{{item2.sectionName}}</el-tag>
                                   <i class="el-icon-caret-right"></i>
                              </el-col> 
                              
                              <!-- <el-col :span="16">
                                  //三级渲染
                                  <el-row v-for="(item3,i3) in item2.childrens" :key="i3"  class="bdboottom itemAligin">
                                    <el-col :span="10">
                                      <el-tag type="warning" >{{item3.sectionName}}</el-tag>
                                       <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="14">
                                        四级渲染 
                                        <el-row v-for="(item4,i4) in item3.childrens" class="bdboottom" :key="i4">
                                          <el-col >
                                              <el-tag type="success" >{{item4.sectionName}}</el-tag>
                                               <i class="el-icon-caret-right"></i>
                                          </el-col> 
                                        </el-row>
                                    </el-col>
                                  </el-row> 
                              </el-col> -->
                              
                           </el-row>
                      </el-col>
                </template>
              </el-row>  
            </template>
        </el-table-column>
        <el-table-column>
          <template slot-scope="{ row }">
            <el-button type="text" @click="handleEditChapter(row)">编辑</el-button>
          </template>
        </el-table-column>
      </template>
    </Tabel>
    <el-dialog
      :title="submitType ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <div>
        <el-form ref="form" :model="formData">
          <el-form-item v-for="item of cols"  :key="item.key" :label="item.label" :prop="item.key">
            <el-input v-if="!item.notAllowEdit" :disabled="item.key=='chapterId'? true : false " v-model="formData[item.key]"></el-input>
            <template  v-else>
              <span>
                {{ item.formatter ? item.formatter(formData) : formData[item.key] }}
              </span>
            </template>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Tabel from '@/components/Table'
  import {
    getChapterList,
    addChapter,
    editChapter,
    deleteChapter,
    getChapterTree
  } from '@/api/TeachingCenter/chapterManage'
  export default {
    name: 'chapterManage',
    components: { Tabel },
    data() { 
      return {
        inputdisable:false,
        searchText:'',
        tableData: [],
        ThreeData:[],
        cols: [
          {
            key: 'chapterId',
            label: '章节编号'
          },
          {
            key: 'chapterName',
            label: '章节名'
          }
          // {
          //   key: 'sectionUrl',
          //   label: '学科名'
          // }
        ],
        multipleSelection: [],
        dialogVisible: false,
        formData: {},
        submitType: null //0：新增,1:修改
      }
    },
    created() {
      this._getChapterList()
      this._getChapterThree()
    },
    methods: {
      async _getChapterList() {
        const { data } = await getChapterList({})
        this.tableData = data.records
        console.log(this.tableData)
      },
      async _getChapterThree(){
        const {data} = await getChapterTree({})
        this.ThreeData= data
        console.log(this.ThreeData)
      },
      handleAddChapter() {
        this.dialogVisible = true
        this.submitType = 0
        this.formData = Object.assign({})
      },
      handleEditChapter(row) {
        this.formData = { ...row }
        this.submitType = 1
        this.dialogVisible = true
      },
      handleDeleteChapter() {
        this.$confirm('是否进行删除操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const ids = this.multipleSelection.map((item) => item.chapterId)
          await deleteChapter({ ids })
          this._getChapterList()
          this.$Msg.success('删除成功!')
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      async submit() {
        const submitArr = [addChapter, editChapter]
        const parmas = this.formData
        await submitArr[this.submitType](parmas)
        this._getChapterList()
        this.$Msg.success('操作成功!')
        this.dialogVisible = false
      }
    }
  }
</script>
<style scoped>
  .itemAligin{
    display: flex;
    align-items: center;
  }
  .el-tag{
    margin: 10px;
  }
  .bdboottom{
    border-bottom: 1px solid #ccc;
  }
</style>